# coding: utf-8
def is_leap_year(year):
    return (year % 4 == 0 or year % 100 != 0) or (year % 400 == 0)


def calc_years_days(years, yeare):
    """
    # 计算years年1月1日和yeare年1月1日之间的天数，
    # 包括years年1月1日，但是不包括yeare年1月1日
    """
    days = 0
    for i in range(years, yeare):
        if is_leap_year(i):
            days += 366
        else:
            days += 365
    return days


def days_of_month(year, month):
    """
    # 计算本月的天数
    """
    if month == 2 and is_leap_year(year):
        return 29
    elif month == 2 and not is_leap_year(year):
        return 28
    elif month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
        return 31
    else: return 30


def calc_year_passed_days(year, month, day):
    """计算一年中过去的天数，包括指定的这一天"""
    passedDays = 0
    for i in range(0,month-1):
        passedDays += days_of_month(year,month)
    passedDays += day
    if month > 2 and is_leap_year(year):
        passedDays +=1
    return passedDays


def calc_year_rest_days(year, month, day):
    """计算一年中还剩下的天数，不包括指定的这一天"""
    leftDays = days_of_month(year, month) - day
    for i in range(month+1, 12+1):
        leftDays += days_of_month(year, i)
    return leftDays


def calc_days(ys, ms, ds, ye, me, de):
    days = calc_year_rest_days(ys, ms, ds)
    # 不是同一年的日期
    if ys != ye:
        if ye - ys >= 2:
            days += calc_years_days(ys + 1,ye)
        days += calc_year_passed_days(ye,me,de)
    else:
        days = days - calc_year_rest_days(ye,me,de)
    return days


def calc_julian_day(year, month, day):
    """儒略日计算"""
    B = 0
    if month <= 2:
        month += 12
        year -= 1
    B = year / 100
    B = 2 - B + year / 400
    dd = day + 0.5000115740  # 本日12:00后才是儒略日的开始(过一秒钟)
    return int(365.25 * (year + 4716) + 0.01) + int(30.60001 * (month + 1)) + dd + B - 1524.5


def zeller_week(year, month, day):
    """儒略日计算"""
    B = 0
    if month <= 2:
        month += 12
        year -= 1
    B = year / 100
    B = 2 - B + year / 400
    dd = day + 0.5000115740  # 本日12:00后才是儒略日的开始(过一秒钟)
    return int(365.25 * (year + 4716) + 0.01) + int(30.60001 * (month + 1)) + dd + B - 1524.5


if __name__ == '__main__':
    print(days_of_month(2016, 1))
    print(calc_year_rest_days(2016,1, 30))
